<?php

class FormBuilder_Element_Input extends FormBuilder_Element
{
	const BUTTON   = 'button';
	const CHECKBOX = 'checkbox';
	const FILE     = 'file';
	const HIDDEN   = 'hidden';
	const IMAGE    = 'image';
	const PASSWORD = 'password';
	const RADIO    = 'radio';
	const RESET    = 'reset';
	const SUBMIT   = 'submit';
	const TEXT     = 'text';

	public function __construct ($type = null, $name = null, $value = null, $attr = null)
	{
		$this->elm[1] = 'type';
		parent::__construct($name, $value, $attr);
		$this->setType($type);
	}

	public function __toString ()
	{
		$format = '<input type="%s" name="%s"%s>';

		$attrs = ''; $this->elm['attributes'] = array_filter($this->elm['attributes']);
		foreach ($this->elm['attributes'] as $attr => $value) {
			$attrs .= sprintf(' %s="%s"', $attr, $value);
		}

		if (!is_null($this->label)) {
			$t = $this->label->render();
			$format = str_replace('[ITEM]', $format, $t);
		}

		return sprintf($format, $this->elm['type'], $this->elm['name'], $attrs);
	}

#------------------------------------------------------------------------------

	public function setType ($type = null)
	{
		$index = array ( self::BUTTON, self::CHECKBOX, self::FILE,
			self::HIDDEN,self::IMAGE, self::PASSWORD, self::RADIO,
			self::RESET, self::SUBMIT, self::TEXT );
		$tpye = defvalue(strtolower($type), self::TEXT);

		if (!in_array($type, $index))
		{
			throw new FormBuilder_Exception();
			return false;
		}

		$this->elm['type'] = $type;
		return $this;
	}

	public function setValue ($value = null)
	{
		return $this->setAttribute('value', $value);
	}

	public function getValue ()
	{
		return $this->getAttribute('value');
	}
}
